using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Chrome;

namespace SeleniumTests
{
    [TestFixture]
    public class SelentoVS
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
        private bool acceptNextAlert = true;
        
        [SetUp]
        public void SetupTest()
        {
           // driver = new ChromeDriver();
           //  driver = new InternetExplorerDriver();
             driver = new FirefoxDriver();
             driver.Manage().Window.Maximize();
            baseURL = "http://pponesource.gmtorque.com/";
            verificationErrors = new StringBuilder();
        }
        
        [TearDown]
        public void TeardownTest()
        {
            try
            {
                driver.Quit();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
            Assert.AreEqual("", verificationErrors.ToString());
        }
        
        [Test]
        public void TheSelentoVSTest()
        {
            driver.Navigate().GoToUrl(baseURL + "/lm");
            System.Threading.Thread.Sleep(10000);
            driver.FindElement(By.Id("UserName")).Clear();
            driver.FindElement(By.Id("UserName")).SendKeys("michaelm");
            driver.FindElement(By.Id("Password")).Clear();
            driver.FindElement(By.Id("Password")).SendKeys("Password1");
            driver.FindElement(By.CssSelector("input.signin.image-button")).Click();
            System.Threading.Thread.Sleep(10000);
            driver.FindElement(By.CssSelector("div.LeadStatus_New")).Click();

            driver.Navigate().GoToUrl(baseURL + "/LM/Mail/MailHome.aspx?Caller=Email%20Home&rep=52531&bil=12547&ent=&NavBarL1=104");
            driver.FindElement(By.Id("106")).Click();
            driver.FindElement(By.Id("ctl00_cpMain_lnkAppointment")).Click();
            System.Threading.Thread.Sleep(10000);
            // ERROR: Caught exception [ERROR: Unsupported command [waitForPopUp | appointment | 30000]]
            // ERROR: Caught exception [ERROR: Unsupported command [selectWindow | name=appointment | ]]
            driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_txSubject")).Clear();
            driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_txSubject")).SendKeys("create personal appt");
            driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_txLocation")).Clear();
            driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_txLocation")).SendKeys("anywhere");
            driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_txComments")).Clear();
            driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_txComments")).SendKeys("comments");
            driver.FindElement(By.LinkText("Save")).Click();
         //   driver.FindElement(By.Id("ctl00_Header1_logout")).Click();
         //   driver.FindElement(By.Id("ctl00_cpMain_hlkLogin")).Click();
        }
          [Test]
        public void TheCalendarTest()
        {
            driver.Navigate().GoToUrl(baseURL + "/LM/Mail/MailHome.aspx?Caller=Email%20Home&rep=52531&bil=12547&ent=&NavBarL1=104");
            driver.FindElement(By.Id("106")).Click();
            driver.FindElement(By.Id("ctl00_cpMain_lnkAppointment")).Click();
            // ERROR: Caught exception [ERROR: Unsupported command [waitForPopUp | appointment | 30000]]
            // ERROR: Caught exception [ERROR: Unsupported command [selectWindow | name=appointment | ]]
            driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_txSubject")).Clear();
            driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_txSubject")).SendKeys("create personal appt");
            driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_txLocation")).Clear();
            driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_txLocation")).SendKeys("anywhere");
            driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_txComments")).Clear();
            driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_txComments")).SendKeys("comments");
            driver.FindElement(By.LinkText("Save")).Click();
        }
        private bool IsElementPresent(By by)
        {
            try
            {
                driver.FindElement(by);
                return true;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }
        
        private string CloseAlertAndGetItsText() {
            try {
                IAlert alert = driver.SwitchTo().Alert();
                if (acceptNextAlert) {
                    alert.Accept();
                } else {
                    alert.Dismiss();
                }
                return alert.Text;
            } finally {
                acceptNextAlert = true;
            }
        }
    }
}
